package com.xuan.controller;

import com.xuan.entity.User;
import com.xuan.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xuan
 * @since 2025-01-14
 */
@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private IUserService userService;

    // 添加用户
    @PostMapping
    public boolean addUser(@RequestBody User user) {
        return userService.save(user);  // 调用服务层的方法进行插入
    }

    @GetMapping("/{id}")
    public User getUser(@PathVariable Long id) {
        return userService.getById(id);  // 通过 ID 获取用户
    }
    @PutMapping("/{id}")
    public boolean updateUser(@PathVariable Long id, @RequestBody User user) {
        user.setId(id);  // 确保更新时使用正确的 ID
        return userService.updateById(user);  // 调用服务层的更新方法
    }
    @DeleteMapping("/{id}")
    public boolean deleteUser(@PathVariable Long id) {
        return userService.removeById(id);  // 调用服务层的删除方法
    }


}

